#include "SnakePch.h"
#include "DSnake.h"

DSnake::DSnake(DWORD dwID)
	:m_pConnector(NULL),
	m_dwID(dwID),
	m_pMover(NULL)
{

}

DSnake::~DSnake()
{

}

bool DSnake::Init( snake::ISnakeConnector* pConnector )
{
	bool		bResult		= false;
	SNAKE_BODY	snakeBody;

	m_pConnector = pConnector;
	//Log(m_pConnector != NULL);

	m_pMover = m_pConnector->GetMover(m_dwID);
	//Log(m_pMover != NULL);

	memset(&snakeBody, 0, sizeof(snakeBody));
	snakeBody.eBodyType = esbtHead;
	m_listSnake.push_front(snakeBody);

	// == Set Born Point == //

	bResult = true;
Exit0:
	return bResult;
}

void DSnake::UnInit()
{
	m_listSnake.clear();
}

void DSnake::Tick()
{
	// m_pMover->Tick();
}

bool DSnake::OnEatFruit( /*Param*/ )
{
	bool									bResult		= false;
	bool									bRetCode	= false;
	std::list<SNAKE_BODY>::reverse_iterator it;

	bRetCode = InsHead();
	//Log(bRetCode);

	it = m_listSnake.rbegin();
	//Log(it != m_listSnake.rend());

	if(it->eBodyType == esbtBody)
		it->eBodyType = esbtTail;

	bResult = true;
Exit0:
	return bResult;
}

bool DSnake::InsHead( /*Param*/ )
{
	bool							bResult	= false;
	SNAKE_BODY						snakeBody;
	std::list<SNAKE_BODY>::iterator	it;

	it = m_listSnake.begin();
	//Log(it != m_listSnake.end());

	memset(&snakeBody, 0, sizeof(snakeBody));

	it->eBodyType = esbtBody;

	snakeBody.eBodyType = esbtHead;
	m_listSnake.push_front(snakeBody);

	bResult = true;
Exit0:
	return bResult;
}

bool DSnake::DelTail()
{
	bool									bResult	= false;
	std::list<SNAKE_BODY>::reverse_iterator it;
	std::list<SNAKE_BODY>::iterator			itTail;

	// Log(m_listSnake.size() > 1);
	
	it = m_listSnake.rbegin();
	//Log(it != m_listSnake.rend());

	itTail = it.base();
	++it;
	m_listSnake.erase(itTail);

	// == New Tail == //
	it->eBodyType = esbtTail;

	bResult = true;
Exit0:
	return bResult;
}
